﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using eeenno.ReaderEngine;
using eeenno.ReaderEngine.Adapters;

namespace eeenno.ReaderEngine.Objects
{
    [Serializable]

    /// <summary>
    /// Collection of Google Reader tag objects
    /// </summary>
    public class Tags
    {
        private const string _TAG_ID_PREFIX = "user/-/label/";

        private List<Tag> _items;

        public Tags()
        {
            _items = new List<Tag>(10);
        }

        public Tags(string[] tags, string[] subscriptions, string[] unread_count)
        {
            _items = new List<Tag>(10);
            Build(tags, subscriptions, unread_count);   
        }        

        public Tag Get(string name)
        {
            for (int i = 0; i < Count; i++)
            {
                if (name == _items[i].name)
                {
                    return _items[i];
                }
            }
            return null;
        }

        public Tag Get(int index)
        {
            return _items[index];
        }

        public void Add(Tag item)
        {
            if (item != null)
            {
                _items.Add(item);
            }
        }

        public int subscription_count
        {
            get
            {
                int count = 0;
                for (int i = 0; i < _items.Count; i++)
                {
                    count += _items[i].subscriptions.Count;
                }
                return count;
            }
        }

        public int Count
        {
            get { return _items.Count; }
        }

        /// <summary>
        /// Builds the tag list
        /// </summary>
        /// <param name="tags">array of available tags</param>
        /// <param name="subscriptions">subscription information</param>
        /// <returns>Google Reader list of tags objects</returns>
        private void Build(string[] tags, string[] subscriptions, string[] unread_count)
        {
            for (int i = 0; i < tags.Length; i++)
            {
                if (!tags[i].Equals("starred") && !tags[i].Equals("broadcast") && !tags[i].Equals("blogger-following"))
                {
                    Tag tag = new Tag();
                    tag.name = tags[i];
                    tag.id = _TAG_ID_PREFIX + tag.name;
                    //prevents untagged reading list from appearing
                    if (tag.name != XmlEngine._NO_TAG_KEYWORD)
                    {
                        tag.subscriptions.Add(new Subscription(tag.name));
                    }

                    for (int j = 0; j < subscriptions.Length; j++)
                    {
                        string[] temp = subscriptions[j].Split('~');
                        if (temp[2].Equals(tags[i]))
                        {
                            Subscription sub = new Subscription();
                            sub.url = temp[0];
                            sub.title = temp[1];
                            sub.tag = temp[2];
                            sub.unread_count = 0;
                            sub.id = sub.url;
                            for (int k = 0; k < unread_count.Length; k++)
                            {
                                temp = unread_count[k].Split('~');
                                if (temp[0].Equals(sub.url))
                                {
                                    sub.unread_count = int.Parse(temp[1]);
                                }
                            }
                            tag.subscriptions.Add(sub);
                        }
                    }
                    tag.RefreshUnreadCount();
                    Add(tag);
                }
            }
        }

        public void MarkAllTagsRead()
        {
            for (int i = 0; i < Count; i++)
            {
                Get(i).subscriptions.MarkAllSubscriptionsRead();
            }
        }

        /// <summary>
        /// Searches through every subscription for the article with the ID, will return null if it doesn't exist
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Article GetArticleByID(string ID)
        {         
            for (int i = 0; i < Count; i++)
            {
                if (Get(i).subscriptions.GetArticleByID(ID) != null)
                {
                    return Get(i).subscriptions.GetArticleByID(ID);
                }
            }
            return null;
        }
    }
}
